package com.fanfanwang.ffw.app.fragment;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.toolbox.NetworkImageView;
import com.fanfanwang.ffw.app.AppConstant;
import com.fanfanwang.ffw.app.AppManager;
import com.fanfanwang.ffw.app.adapter.GlyCarportStatusAdapter;
import com.fanfanwang.ffw.app.asynctask.GetParknumListAsyncTask;
import com.fanfanwang.ffw.app.base.BaseFragment;
import com.fanfanwang.ffw.app.bean.CarportBean;
import com.fanfanwang.ffw.app.bean.MsgListBean;
import com.fanfanwang.ffw.app.bean.ParkNumBean;
import com.fanfanwang.ffw.app.bean.UserBean;
import com.fanfanwang.ffw.app.fragment.GlyMessageFragment.IgetMessageList;
import com.fanfanwang.ffw.app.tenant.R;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.ui.login.LoginAct;
import com.fanfanwang.ffw.app.ui.my.AccountAndSafetyAct;
import com.fanfanwang.ffw.app.ui.my.MyUpdateInfoAct;
import com.fanfanwang.ffw.app.widget.CommonTitleBar;

@SuppressLint("ValidFragment")
public class GlyHomeFragment extends BaseFragment {
	private LayoutInflater inflater;
	private CommonTitleBar commonTitleBar;
	private ListView listview;
	private Context context;
	private int i=0;//0表示第一个显示，1表示第二个显示，2表示第三个fragement显示。
	private GlyCarportStatusAdapter carportStatusAdapter;
	private ArrayList<ParkNumBean> carportBeans;
//	private UserBean userBean;
	private TextView carport_name,carport_add,set_charging_stand,set_charging_time,set_parking_type;
	public static IgetParknumList iGetParknumList;
	@SuppressLint("ValidFragment")
	public GlyHomeFragment(Context context) {
		// TODO Auto-generated constructor stub
		super();
		inflater = LayoutInflater.from(context);
		this.context=context;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View settingLayout = inflater.inflate(
				R.layout.gly_activity_carport_state, container, false);
//		Bundle bundle=getArguments();
//		if(bundle!=null)
//		{
//			this.userBean=(UserBean) bundle.getSerializable("data");
//		}
		return settingLayout;
	}
	public static GlyHomeFragment getHomeFragment(Context context) {

		GlyHomeFragment  mainMyFragment = new GlyHomeFragment(context);
		return mainMyFragment;
	}
	@SuppressLint("NewApi")
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		getActivity().getWindow().setFormat(PixelFormat.TRANSLUCENT);
		initView(view);
		initData();
		setListener();
	}

	@Override
	public void initView(View view) {
		commonTitleBar = (CommonTitleBar) view.findViewById(R.id.common_titlebar);
		listview=(ListView)view.findViewById(R.id.lv_carport);
		carportBeans=new ArrayList<ParkNumBean>();
		carport_name=(TextView)view.findViewById(R.id.carport_name);
		carport_add=(TextView)view.findViewById(R.id.carport_add);
		set_charging_stand=(TextView)view.findViewById(R.id.set_charging_stand);
		set_charging_time=(TextView)view.findViewById(R.id.set_charging_time);
		set_parking_type=(TextView)view.findViewById(R.id.set_parking_type);
	}

	@Override
	public void initData() {
		commonTitleBar.setCenterText(getString(R.string.main_tab_gly_home));
		//更新数据。
		String name=SharedPreferencesUtil.getInstance(getActivity()).getName();
		if(!TextUtils.isEmpty(name)){
			carport_name.setText(name);
		}
		String add=SharedPreferencesUtil.getInstance(getActivity()).getAddress();
		if(!TextUtils.isEmpty(add)){
			carport_add.setText(add);
		}
		StringBuffer sb=new StringBuffer();
		String chargePrice=SharedPreferencesUtil.getInstance(getActivity()).getChargeprice();
		if(!TextUtils.isEmpty(chargePrice)){
			sb.append(chargePrice+"元");
		}
		String chargetype=SharedPreferencesUtil.getInstance(getActivity()).getChargetype();
		if(!TextUtils.isEmpty(chargetype)){
			if(chargetype.equals("0"))
			{
				sb.append("/次");
			}else{
				sb.append("/时");
			}
		}
		//收费标准
		set_charging_stand.setText(sb.toString());
		
		if(!TextUtils.isEmpty(SharedPreferencesUtil.getInstance(getActivity()).getOpentime())){
			//收费时间
			set_charging_time.setText(SharedPreferencesUtil.getInstance(getActivity()).getOpentime());
		}
		if(!TextUtils.isEmpty(SharedPreferencesUtil.getInstance(getActivity()).getParktype())){
			//泊车类型
			set_parking_type.setText(SharedPreferencesUtil.getInstance(getActivity()).getParktype());
		}
		setInterface();
		new GetParknumListAsyncTask(getActivity(), SharedPreferencesUtil.getInstance(getActivity()).getParkingId()).execute("");
		carportStatusAdapter=new GlyCarportStatusAdapter(getActivity(), carportBeans);
		listview.setAdapter(carportStatusAdapter);
		
	}
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
	/**
	 *  提示对话框
	 * @param message
	 */
	public void showLogoutDialog(String message){
		new AlertDialog.Builder(context).setTitle(getString(R.string.exit_title))  
		.setMessage(message)
		.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener(){
			public void onClick(DialogInterface dialog, int which) {
				//sharedPreferencesUtil.putIsLogin(false);
				/*sharedPreferencesUtil.putUserId(null);
			sharedPreferencesUtil.putMobile(null);
			sharedPreferencesUtil.putUserNickName(null);*/
				//		SharedPreferencesUtil.getInstance(PersonalSetAct.this).putUserName(null);
				//				SharedPreferencesUtil.getInstance(PersonalSetAct.this).putUserId(null);
				//				//		SharedPreferencesUtil.getInstance(PersonalSetAct.this).putHeadUrl(null);
				//				AppConstant.user_id = "";
				//				//		AppConstant.username = "";
				//				AppConstant.user_nickName = "";
				//				//		AppConstant.headimg = "";
				AppManager.getAppManager().finishAllActivity();
				Intent mIntent = new Intent(getActivity(), LoginAct.class);
				startActivity(mIntent);
				getActivity().finish();
			}
		})
		.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		})
		.show();
	}
	@Override
	public void setListener() {
	}

	public interface IgetParknumList{
		public void getParknumList(ArrayList<ParkNumBean> beans);
	}
	public void setInterface(){
		iGetParknumList=new IgetParknumList() {
			
			@Override
			public void getParknumList(ArrayList<ParkNumBean> beans) {
				// TODO Auto-generated method stub
				carportStatusAdapter.resetData(beans);
				carportStatusAdapter.notifyDataSetChanged();
			}
		};
	}
}
